/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.WorkerAcceptanceUtil;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_006_Util;
import org.ourgrid.acceptance.util.worker.Req_079_Util;
import org.ourgrid.acceptance.util.worker.Req_080_Util;
import org.ourgrid.acceptance.util.worker.Req_081_Util;
import org.ourgrid.acceptance.util.worker.Req_084_Util;
import org.ourgrid.acceptance.util.worker.Req_090_Util;
import org.ourgrid.acceptance.util.worker.Req_092_Util;
import org.ourgrid.acceptance.util.worker.Req_094_Util;
import org.ourgrid.acceptance.util.worker.Req_121_Util;
import org.ourgrid.acceptance.util.worker.Req_122_Util;
import org.ourgrid.common.executor.IntegerExecutorHandle;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.common.interfaces.WorkerClient;
import org.ourgrid.common.interfaces.management.RemoteWorkerManagementClient;
import org.ourgrid.common.interfaces.management.WorkerManagementClient;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;
import org.ourgrid.worker.WorkerConfiguration;
import org.ourgrid.worker.dao.EnvironmentDAO;
import org.ourgrid.worker.utils.RandomNumberUtil;

import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.processor.filetransfer.IncomingTransferHandle;
import br.edu.ufcg.lsd.commune.processor.filetransfer.OutgoingTransferHandle;
import br.edu.ufcg.lsd.commune.processor.filetransfer.TransferHandle;

@ReqTest(reqs="REQ122")
public class Req_122_Test extends WorkerAcceptanceTestCase {
	
	private DeploymentID peerID;
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_090_Util req_090_Util = new Req_090_Util(getComponentContext());
	private Req_092_Util req_092_Util = new Req_092_Util(getComponentContext());
	private Req_080_Util req_080_Util = new Req_080_Util(getComponentContext());
	private Req_081_Util req_081_Util = new Req_081_Util(getComponentContext());
	private Req_084_Util req_084_Util = new Req_084_Util(getComponentContext());
	private Req_079_Util req_079_Util = new Req_079_Util(getComponentContext());
	private Req_006_Util req_006_Util = new Req_006_Util(getComponentContext());
	private Req_121_Util req_121_Util = new Req_121_Util(getComponentContext());
	private Req_122_Util req_122_Util = new Req_122_Util(getComponentContext());
	private Req_094_Util req_094_Util = new Req_094_Util(getComponentContext());
		
	private static final String playpenDirName  = "worker-4962768465676381896";
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		String peerPubKey = workerAcceptanceUtil.simulateAuthentication();
		peerID = new DeploymentID(new ContainerID("username", "server", "module", peerPubKey), "peer");
		
	}

	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. Notify failure of the peer "unknownPeerID";
	 *           * Verify if the following warn message was logged: The unknown peer [unknownPeerID] has failed. This message was ignored.
	 * 
	 */
	@ReqTest(test=" AT-122.1", reqs="REQ122")
	@Test public void test_at_122_1_UnknownPeerFails() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();

		//Notify failure of the peer "unknownPeerID"
		DeploymentID unknownPeerID = new DeploymentID(new ContainerID("username", "server", "module", "unknownPublicKey"), "peer");
		req_122_Util.unknownPeerFails(component, unknownPeerID);
	}
	
	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. Verify if the Worker is not interested on master peer failure
	 *    3. Notify failure of the master peer;
	 *           * Verify if the following warn message was logged: The peer [peerID] that didn't set itself as manager of this Worker has failed. This message was ignored.
	 *    4. Verify if the Worker is not interested on master peer failure
	 * 
	 */
	@ReqTest(test=" AT-122.2", reqs="REQ122")
	@Test public void test_at_122_2_PeerFailsBeforeSetPeer() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();

		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Notify failure of the peer "peerID"
		req_122_Util.peerFailsBeforeSetPeer(component, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
	}
	
	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    3. Verify if the Worker is interested on master peer failure
	 *    4. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *    5. Verify if the Worker is not interested on master peer failure
	 *    6. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    7. The master peer commands the Worker to work for the client with the public key "mgPublicKey1";
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a local consumer. Local consumer public key: [mgPublicKey1]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *    8. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *    9. Verify if the Worker is not interested on master peer failure
	 *   10. Verify if the worker status is IDLE;
	 *   11. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *   12. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   13. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *   14. Verify if the Worker is not interested on master peer failure
	 *   15. Verify if the worker status is IDLE;
	 *   16. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *   17. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   18. The remote peer with the public key "peerPubKey" commands the Worker to work for the remote client with the public key "mgPublicKey222";
	 *           * Verify if the following info message was logged: "Remote peer commanded this Worker to work for a remote consumer. Remote consumer public key: [mgPublicKey222]."
	 *           * Verify if the following debug message was logged: "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the remote peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *   19. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: The master peer [peerID] notified failure. Worker will interrupt the working, it means cancel any transfer or execution.
	 *   20. Verify if the Worker is not interested on master peer failure
	 *   21. Verify if the worker status is IDLE;
	 *   
	 */
	@ReqTest(test=" AT-122.3", reqs="REQ122")
	@Test public void test_at_122_3_PeerFailsAfterSetPeer() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();
		
		//Master peer sends a setPeer message
		WorkerManagementClient wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//Verify if the Worker is interested on master peer failure
		assertTrue(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Notify failure of the peer "peerID"
		req_122_Util.peerFailsAfterSetPeer(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer commands the Worker to work for the client with the public key "mgPublicKey1"
		req_092_Util.workForMyGridOnIdleWorker(component, wmc, "mgPublicKey1");
		req_092_Util.beginAllocationCompleted(component, wmc);
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsWithCleaningWorker(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsWithCleaningWorker(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//The remote peer with the public key "peerPubKey" commands the Worker to work for
		// the remote client with the public key "mgPublicKey222"
		req_121_Util.workForMyGridOnAllocatedForPeerWorker(component, "peerPubKey", "mgPublicKey222");
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsWithCleaningWorker(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}

	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    3. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    4. The master peer commands the Worker to work for the client with the public key "mgPublicKey1";
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a local consumer. Local consumer public key: [mgPublicKey1]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *    5. A client with the public key "mgPublicKey1" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *    6. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *    7. Verify if the Worker is not interested on master peer failure
	 *    8. Verify if the worker status is IDLE;
	 *    9. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   10. The remote peer with the public key "peerPubKey" commands the Worker to work for the remote client with the public key "mgPublicKey222";
	 *           * Verify if the following info message was logged: "Remote peer commanded this Worker to work for a remote consumer. Remote consumer public key: [mgPublicKey222]."
	 *           * Verify if the following debug message was logged: "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the remote peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *   11. A client with the public key "mgPublicKey222" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID1].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *   12. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *   13. Verify if the Worker is not interested on master peer failure
	 *   14. Verify if the worker status is IDLE;
	 * 
	 */
	@ReqTest(test=" AT-122.4", reqs="REQ122")
	@Test public void test_at_122_4_PeerFailsAndWorkerHasStartedWorking() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();
		
		//Master peer sends a setPeer message
		WorkerManagementClient wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer commands the Worker to work for the client with the public key "mgPublicKey1"
		req_092_Util.workForMyGridOnIdleWorker(component, wmc, "mgPublicKey1");
		Worker worker = req_092_Util.beginAllocationCompleted(component, wmc);
		
		//A client with the public key "mgPublicKey1" sends a startWork message
		DeploymentID myGrid1ID = new DeploymentID(new ContainerID("mgUserName", "mgServer", "mgModule", "mgPublicKey1"), "myGrid");
		
		req_079_Util.startWorkSuccessfully(component, worker, myGrid1ID);
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsWithCleaningWorker(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//The remote peer with the public key "peerPubKey" commands the Worker to work for
		// the remote client with the public key "mgPublicKey222"
		RemoteWorkerManagementClient rwmc = req_121_Util.workForMyGridOnAllocatedForPeerWorker(component, "peerPubKey", "mgPublicKey222");
		worker = req_121_Util.beginAllocationCompleted(component, rwmc);
		
		//A client with the public key "mgPublicKey222" sends a startWork message
		DeploymentID myGrid2ID = new DeploymentID(new ContainerID("mg2UserName", "mg2Server", "mg2Module", "mgPublicKey222"), "myGrid2");
		
		req_079_Util.startWorkSuccessfully(component, worker, myGrid2ID);
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsWithCleaningWorker(component, wmc, peerID);
		
		//Verify if the Worker is not interested on master peer failure
		assertFalse(req_122_Util.isWorkerInterestedOnMasterPeerFailure(component, peerID));
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}
	
	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    3. The master peer commands the Worker to work for the client with the public key "mgPublicKey1";
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a local consumer. Local consumer public key: [mgPublicKey1]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *    4. A client with the public key "mgPublicKey1" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *    5. A client with the public key "mgPublicKey1" requests to transfer the file "file1", with size 1123 bytes and the handle "0";
	 *           * Verify if the following debug message was logged: A transfer request from the client was successfully accepted. Client public key: [mgPublicKey1]. File destination: [playpenDir/file1]. File size: 1123 bytes. Handle: 0.
	 *           * Expect the Worker to accept the file transfer;
	 *    6. A client with the public key "mgPublicKey1" requests to transfer the file "file2", with size 1123 bytes and the handle "1";
	 *           * Verify if the following debug message was logged: A transfer request from the client was successfully accepted. Client public key: [mgPublicKey1]. File destination: [playpenDir/file2]. File size: 1123 bytes. Handle: 1.
	 *           * Expect the Worker to accept the file transfer;
	 *    7. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the incoming file transfer with the handle "0";
	 *           * Expect the Worker to cancel the incoming file transfer with the handle "1";
	 *    8. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    9. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   10. The remote peer with the public key "peerPubKey" commands the Worker to work for the remote client with the public key "mgPublicKey222";
	 *           * Verify if the following info message was logged: "Remote peer commanded this Worker to work for a remote consumer. Remote consumer public key: [mgPublicKey222]."
	 *           * Verify if the following debug message was logged: "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the remote peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *   11. A client with the public key "mgPublicKey222" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID1].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *   12. A client with the public key "mgPublicKey222" requests to transfer the file "file1", with size 1123 bytes and the handle "0";
	 *           * Verify if the following debug message was logged: A transfer request from the client was successfully accepted. Client public key: [mgPublicKey222]. File destination: [playpenDir/file1]. File size: 1123 bytes. Handle: 0.
	 *           * Expect the Worker to accept the file transfer;
	 *   13. A client with the public key "mgPublicKey222" requests to transfer the file "file2", with size 1123 bytes and the handle "1";
	 *           * Verify if the following debug message was logged: A transfer request from the client was successfully accepted. Client public key: [mgPublicKey1]. File destination: [playpenDir/file2]. File size: 1123 bytes. Handle: 1.
	 *           * Expect the Worker to accept the file transfer;
	 *   14. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the incoming file transfer with the handle "0";
	 *           * Expect the Worker to cancel the incoming file transfer with the handle "1";
	 * 
	 */
	@ReqTest(test=" AT-122.5", reqs="REQ122")
	@Test public void test_at_122_5_PeerFailsWhileWorkerIsReceivingFiles() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();
		
		//Master peer sends a setPeer message
		WorkerManagementClient wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer commands the Worker to work for the client with the public key "mgPublicKey1"
		req_092_Util.workForMyGridOnIdleWorker(component, wmc, "mgPublicKey1");
		Worker worker = req_092_Util.beginAllocationCompleted(component, wmc);
		
		//A client with the public key "mgPublicKey1" sends a startWork message
		DeploymentID myGrid1ID = new DeploymentID(new ContainerID("mgUserName", "mgServer", "mgModule", "mgPublicKey1"), "myGrid");
		
		req_079_Util.startWorkSuccessfully(component, worker, myGrid1ID);

		//A client with the public key "mgPublicKey1" requests to transfer the file "file1",
		// with size 1123 bytes and the handle "0"
		IncomingTransferHandle handle0 = new IncomingTransferHandle(0, "file1", 1123, "file1", "PUT", myGrid1ID.getContainerID());
		req_080_Util.requestToTransferFile(component, 1, myGrid1ID.getContainerID(), "file1", handle0, 1123);
		
		//A client with the public key "mgPublicKey1" requests to transfer the file "file2",
		// with size 1123 bytes and the handle "1"
		IncomingTransferHandle handle1 = new IncomingTransferHandle(1, "file2", 1123, "file2", "PUT", myGrid1ID.getContainerID());
		req_080_Util.requestToTransferFile(component, 1, myGrid1ID.getContainerID(), "file2", handle1, 1123);
		
		//Notify failure of the master peer "peerID"
		List<TransferHandle> handles = new ArrayList<TransferHandle>();
		handles.add(handle0);
		handles.add(handle1);
		
		req_122_Util.peerFailsAndWorkerHasIncomingTransfer(component, wmc, peerID, handles);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//The remote peer with the public key "peerPubKey" commands the Worker to work for
		// the remote client with the public key "mgPublicKey222"
		RemoteWorkerManagementClient rwmc = req_121_Util.workForMyGridOnAllocatedForPeerWorker(component, "peerPubKey", "mgPublicKey222");
		worker = req_121_Util.beginAllocationCompleted(component, rwmc);
		
		//A client with the public key "mgPublicKey222" sends a startWork message
		DeploymentID myGrid2ID = new DeploymentID(new ContainerID("mg2UserName", "mg2Server", "mg2Module", "mgPublicKey222"), "myGrid2");
		
		req_079_Util.startWorkSuccessfully(component, worker, myGrid2ID);

		//A client with the public key "mgPublicKey1" requests to transfer the file "file1",
		// with size 1123 bytes and the handle "0"
		req_080_Util.requestToTransferFileByUnknownClient(component, 1, myGrid1ID.getContainerID(), "file1", handle0, 1123);
		
		//A client with the public key "mgPublicKey1" requests to transfer the file "file2",
		// with size 1123 bytes and the handle "1"
		req_080_Util.requestToTransferFileByUnknownClient(component, 1, myGrid1ID.getContainerID(), "file2", handle1, 1123);
	
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsAndWorkerHasIncomingTransfer(component, wmc, peerID, handles);
	}
	
	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    3. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    4. The master peer commands the Worker to work for the client with the public key "mgPublicKey1";
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a local consumer. Local consumer public key: [mgPublicKey1]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *    5. A client with the public key "mgPublicKey1" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *    6. A client with the public key "mgPublicKey1" tries to execute the remote command "echo foo"; without releasing the handle (execution blocks)
	 *           * Verify if the following info message was logged: "Command scheduled to execution. Command: echo foo; RequestID: requestID ; Environment variables: ; Client public key: [mgPublicKey1].
	 *    7. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the execution;
	 *    8. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    9. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   10. The remote peer with the public key "peerPubKey" commands the Worker to work for the remote client with the public key "mgPublicKey222";
	 *           * Verify if the following info message was logged: "Remote peer commanded this Worker to work for a remote consumer. Remote consumer public key: [mgPublicKey222]."
	 *           * Verify if the following debug message was logged: "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the remote peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *   11. A client with the public key "mgPublicKey222" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID1].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *   12. A client with the public key "mgPublicKey1" tries to execute the remote command "echo foo"; without releasing the handle (execution blocks)
	 *           * Verify if the following info message was logged: "Command scheduled to execution. Command: echo foo; RequestID: requestID ; Environment variables: ; Client public key: [mgPublicKey1].
	 *   13. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the execution;
	 * 
	 */
	@ReqTest(test=" AT-122.6", reqs="REQ122")
	@Test public void test_at_122_6_PeerFailsWhileWorkerIsExecutingCommand() throws Exception {
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker();
		
		//Master peer sends a setPeer message
		WorkerManagementClient wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer commands the Worker to work for the client with the public key "mgPublicKey1"
		req_092_Util.workForMyGridOnIdleWorker(component, wmc, "mgPublicKey1");
		Worker worker = req_092_Util.beginAllocationCompleted(component, wmc);
		
		//A client with the public key "mgPublicKey1" sends a startWork message
		DeploymentID myGrid1ID = new DeploymentID(new ContainerID("mgUserName", "mgServer", "mgModule", "mgPublicKey1"), "myGrid");
		
		RandomNumberUtil.setSeed(0);
		WorkerClient workerClient = req_079_Util.startWorkSuccessfully(component, worker, myGrid1ID);
		
		//A client with the public key "mgPublicKey1" tries to execute the remote command "echo foo"
		//The WorkerExecutionClient does not receive an execution result
		long requestID = 1;
		Map<String, String> envVars = new HashMap<String, String>();
		
		
		Future<?> future = req_084_Util.remoteExecutionWithNoExecutionResult(component, worker, workerClient, myGrid1ID,
				requestID, envVars, "echo echo", 0);
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsAndWorkerIsExecutingACommand(component, wmc, peerID, future);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//The remote peer with the public key "peerPubKey" commands the Worker to work for
		// the remote client with the public key "mgPublicKey222"
		RemoteWorkerManagementClient rwmc = req_121_Util.workForMyGridOnAllocatedForPeerWorker(component, "peerPubKey", "mgPublicKey222");
		worker = req_121_Util.beginAllocationCompleted(component, rwmc);
		
		//A client with the public key "mgPublicKey222" sends a startWork message
		DeploymentID myGrid2ID = new DeploymentID(new ContainerID("mg2UserName", "mg2Server", "mg2Module2", "mgPublicKey222"), "myGrid2");
		
		RandomNumberUtil.setSeed(0);
		workerClient = req_079_Util.startWorkSuccessfully(component, worker, myGrid2ID);
		
		//A client with the public key "mgPublicKey1" tries to execute the remote command "echo foo"
		//The WorkerExecutionClient does not receive an execution result
		req_084_Util.getWorkerExecutionClient().executionIsRunning(null);
		
		future = req_084_Util.remoteExecutionWithNoExecutionResult(component, worker, workerClient, myGrid2ID,
				requestID, envVars, "echo echo", 2);
		
		req_084_Util.getWorkerExecutionClient().executionIsRunning(new IntegerExecutorHandle(0));
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsAndWorkerIsExecutingACommand(component, wmc, peerID, future);
	}
	
	/**
	 *    1.  Start a Worker with the idleness detector DISABLED;
	 *    2. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    3. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    4. The master peer commands the Worker to work for the client with the public key "mgPublicKey1";
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a local consumer. Local consumer public key: [mgPublicKey1]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *    5. A client with the public key "mgPublicKey1" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *    6. A client with the public key "mgPublicKey1" tries to execute the remote command "touch $PLAYPEN/foo1";
	 *           * Verify if the following info message was logged: "Command scheduled to execution. Command: touch playpenDir/foo1; RequestID: requestID ; Environment variables: ; Client public key: [mgPublicKey1].
	 *           * Expect if the worker schedules an execution action HELP. The execution directory should be the playpen.
	 *           * Expect the schedule action to send a executionResult to the WorkerClient
	 *    7. Copy the file "foo1" to playpen directory
	 *    8. Copy the file "foo2" to playpen directory
	 *    9. A client with the public key "mgPublicKey1" requests to recover the file "foo1", "foo2";
	 *           * Verify if the following debug message was logged: The client tried to recover the files. The worker accepted the transfer request and is starting to upload the file. File path: [playpenDir/foo1]. Handle: 1. Client public key: [mgPublicKey1].
	 *           * Expect the Worker to start the transfer;
	 *           * Verify if the following debug message was logged: The client tried to recover the files. The worker accepted the transfer request and is starting to upload the file. File path: [playpenDir/foo2]. Handle: 2. Client public key: [mgPublicKey1].
	 *           * Expect the Worker to start the transfer;
	 *   10. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the outgoing file transfer with the handle "1";
	 *           * Expect the Worker to cancel the outgoing file transfer with the handle "2";
	 *   11. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *   12. The master peer sends a workForPeer message to the Worker;
	 *           * Verify if the following info message was logged: "Peer commanded this Worker to work for a remote peer. Remote peer public key: [peerPubKey]."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to ALLOCATED_FOR_PEER."
	 *           * Expect the Worker to send the peer an allocated for peer status changed message, with a stub to the Remote Worker Management remote object
	 *   13. The remote peer with the public key "peerPubKey" commands the Worker to work for the remote client with the public key "mgPublicKey222";
	 *           * Verify if the following info message was logged: "Remote peer commanded this Worker to work for a remote consumer. Remote consumer public key: [mgPublicKey222]."
	 *           * Verify if the following debug message was logged: "Status changed from ALLOCATED_FOR_PEER to ALLOCATED_FOR_MYGRID."
	 *           * Expect the Worker to send the remote peer an allocated for mygrid status changed message, with a stub to the Worker remote object
	 *   14. A client with the public key "mgPublicKey222" sends a startWork message;
	 *           * Verify if the following info message was logged: Worker is ready to start working for client [ClientID1].
	 *           * Expect the Worker to send the client a Worker is Ready message
	 *   15. A client with the public key "mgPublicKey1" tries to execute the remote command "touch $PLAYPEN/foo1";
	 *           * Verify if the following info message was logged: "Command scheduled to execution. Command: touch playpenDir/foo1; RequestID: requestID ; Environment variables: ; Client public key: [mgPublicKey1].
	 *           * Expect if the worker schedules an execution action HELP. The execution directory should be the playpen.
	 *           * Expect the schedule action to send a executionResult to the WorkerClient
	 *   16. Copy the file "foo1" to playpen directory
	 *   17. Copy the file "foo2" to playpen directory
	 *   18. A client with the public key "mgPublicKey222" requests to recover the file "foo1", "foo2";
	 *           * Verify if the following debug message was logged: The client tried to recover the files. The worker accepted the transfer request and is starting to upload the file. File path: [playpenDir/foo1]. Handle: 1. Client public key: [mgPublicKey222].
	 *           * Expect the Worker to start the transfer;
	 *           * Verify if the following debug message was logged: The client tried to recover the files. The worker accepted the transfer request and is starting to upload the file. File path: [playpenDir/foo2]. Handle: 2. Client public key: [mgPublicKey222].
	 *           * Expect the Worker to start the transfer;
	 *   19. Notify failure of the master peer;
	 *           * Verify if the following debug message was logged: "Cleaning Worker."
	 *           * Verify if the following debug message was logged: The master peer [peerID] has failed. Worker will interrupt the working, it means cancel any transfer or execution.
	 *           * Expect the Worker to cancel the outgoing file transfer with the handle "1";
	 *           * Expect the Worker to cancel the outgoing file transfer with the handle "2";
	 * 
	 */
	@ReqTest(test=" AT-122.7", reqs="REQ122")
	@Test public void test_at_122_7_PeerFailsWhileWorkerIsSendingFiles() throws Exception {
		//Start Worker with idleness detector DISABLED
		Map<String, Object> properties = workerAcceptanceUtil.createWorkerProperties(false);
		WorkerComponent component = req_004_Util.startWorker();
		
		//Master peer sends a setPeer message
		WorkerManagementClient wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer commands the Worker to work for the client with the public key "mgPublicKey1"
		req_092_Util.workForMyGridOnIdleWorker(component, wmc, "mgPublicKey1");
		Worker worker = req_092_Util.beginAllocationCompleted(component, wmc);
		
		//A client with the public key "mgPublicKey1" sends a startWork message
		DeploymentID myGrid1ID = new DeploymentID(new ContainerID("mgUserName", "mgServer", "mgModule", "mgPublicKey1"), "myGrid");
		
		RandomNumberUtil.setSeed(0);
		WorkerClient workerClient = req_079_Util.startWorkSuccessfully(component, worker, myGrid1ID);

		//A client with the public key "mgPublicKey1" tries to execute the remote command "touch $PLAYPEN/foo1"
		String playpenPath = component.getDAO(EnvironmentDAO.class).getPlaypenDir();
		File pDir = new File(playpenPath);
		
		String storagePath = component.getDAO(EnvironmentDAO.class).getStorageDir();
		File sDir = new File(storagePath);
		
		long requestID = 1;
		Map<String, String> envVars = new HashMap<String, String>();
		String playpenDir = pDir.getAbsolutePath();
		String storageDir = sDir.getAbsolutePath();
		envVars.put("STORAGE", storageDir);
		envVars.put("PLAYPEN", playpenDir);
		
		String storageRoot = (String) properties.get(WorkerConfiguration.PROP_STORAGE_DIR);

		req_084_Util.getWorkerExecutionClient().executionIsRunning(null);
		
		req_084_Util.remoteExecutionWithSuccess(component, worker, workerClient, myGrid1ID, 
				requestID, envVars, "touch $PLAYPEN/foo1;", playpenDir, storageDir, 0);
		
		//Copy the file "foo1" to playpen directory
		String completeFile1Path = playpenDir + File.separator + "foo1";
		WorkerAcceptanceUtil.createFile(completeFile1Path);
		
		//Copy the file "foo2" to playpen directory
		String completeFile2Path = playpenDir + File.separator + "foo2";
		WorkerAcceptanceUtil.createFile(completeFile2Path);
		
		//A client with the public key "mgPublicKey1" requests to recover the file "foo1"
		req_081_Util.requestToRecoverFilesWithSuccess(component, requestID, myGrid1ID.getPublicKey(), workerClient,
				playpenDir, storageDir, "foo1");
		
		//A client with the public key "mgPublicKey1" requests to recover the file "foo2"
		req_081_Util.requestToRecoverFilesWithSuccess(component, requestID, myGrid1ID.getPublicKey(), workerClient,
				playpenDir, storageDir, "foo2");
		
		//Notify failure of the master peer "peerID"
		OutgoingTransferHandle handle0 = new OutgoingTransferHandle(0, "foo1", "foo1", "", myGrid1ID);
		OutgoingTransferHandle handle1 = new OutgoingTransferHandle(1, "foo2", "foo2", "", myGrid1ID);
		
		List<TransferHandle> handles = new ArrayList<TransferHandle>();
		handles.add(handle0);
		handles.add(handle1);
		
		req_122_Util.peerFailsAndWorkerHasOutgoingTransfer(component, wmc, peerID, handles);
		
		//Master peer sends a setPeer message
		wmc = req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		//The master peer sends a workForPeer message to the Worker
		req_006_Util.workForPeerOnIdleWorker(component, wmc, peerID.getPublicKey(), "peerPubKey");
		
		//The remote peer with the public key "peerPubKey" commands the Worker to work for
		// the remote client with the public key "mgPublicKey222"
		RemoteWorkerManagementClient rwmc = req_121_Util.workForMyGridOnAllocatedForPeerWorker(component, "peerPubKey", "mgPublicKey222");
		worker = req_121_Util.beginAllocationCompleted(component, rwmc);
		
		//A client with the public key "mgPublicKey222" sends a startWork message
		DeploymentID myGrid2ID = new DeploymentID(new ContainerID("mg2UserName", "mg2Server", "mg2Module", "mgPublicKey222"), "myGrid2");
		
		RandomNumberUtil.setSeed(0);
		workerClient = req_079_Util.startWorkSuccessfully(component, worker, myGrid2ID);
		
		//Copy the file "foo1" to playpen directory
		WorkerAcceptanceUtil.createFile(completeFile1Path);
		
		//Copy the file "foo2" to playpen directory
		WorkerAcceptanceUtil.createFile(completeFile2Path);
		
		//A client with the public key "mgPublicKey222" tries to execute the remote command "touch $PLAYPEN/foo1"
		storageDir = storageRoot + File.separator + WorkerAcceptanceUtil.generateHexadecimalCodedString(
				myGrid2ID.getPublicKey());
		req_084_Util.remoteExecutionWithSuccess(component, worker, workerClient, myGrid2ID,
				requestID, envVars, "touch $PLAYPEN/foo1;", playpenDir, storageDir, 1);
		
		//A client with the public key "mgPublicKey22" requests to recover the file "foo1"
		req_081_Util.requestToRecoverFilesWithSuccess(component, requestID, myGrid2ID.getPublicKey(), workerClient,
				playpenDir, storageDir, "foo1");
		
		//A client with the public key "mgPublicKey222" requests to recover the file "foo2"
		req_081_Util.requestToRecoverFilesWithSuccess(component, requestID, myGrid2ID.getPublicKey(), workerClient,
				playpenDir, storageDir, "foo2");
		
		//Notify failure of the master peer "peerID"
		req_122_Util.peerFailsAndWorkerHasOutgoingTransfer(component, wmc, peerID, handles);
	}
	
}